/*
 * Copyright (c) 2009, Andrey Shvetsov
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#ifndef _XINTLIBIO_H
#define	_XINTLIBIO_H


#include <xintlib.h>

#include <string>
#include <ostream>
#include <iomanip>
#include <sstream>


namespace xintlib {

    template <class XINT>
    struct hex_writer {
        typedef typename XINT::unit_type unit_type;

        hex_writer(std::ostream &os, std::string delimiter): _os(os), _delimiter(delimiter) {}

        void operator()(unit_type u) const {
            std::stringstream ss;
            ss.fill( '0' );
            ss << std::hex << std::setw( 2*sizeof( unit_type ) ) << u+0; /*
             * '+0' is used to increase type size in case of unit_type==char
             */
            _os << ss.str() << _delimiter;
        }

        std::ostream &_os;
        const std::string _delimiter;
    };

    struct hex_ostream_t {
        hex_ostream_t(std::ostream &os, const char *delimiter): _os(os), _delimiter(delimiter) {}
        std::ostream &_os;
        const char *const _delimiter;
    };

    struct hex {
        hex(const char *delimiter = ""): _delimiter( delimiter ) {}
        const char *const _delimiter;
    };

    inline hex_ostream_t operator<<(std::ostream &os, const hex &hx)
    {
        return hex_ostream_t( os, hx._delimiter );
    }

    template <class XINT>
    inline std::ostream &operator<<(hex_ostream_t xos, const XINT &x)
    {
        for_each_unit( x, hex_writer<XINT>( xos._os, xos._delimiter ) );
        return xos._os;
    }

} // namespace xintlib


namespace xintlib {

    template <class XINT>
    struct hex_reader {
        typedef typename XINT::unit_type unit_type;

        hex_reader(const char *hex_str): _str_begin( hex_str ) {
            _str = _str_begin;
            while( *_str ) ++_str;
        }

        void operator()(unit_type &u) {
            unit_type sum = 0;
            int sh = 0;
            while( _str != _str_begin ) {
                sum += _c2n( *--_str ) << sh;
                if( ( sh += 4 ) >= 8*sizeof( unit_type ) )
                    break;
            }
            u = sum;
        }

    protected:
        static int _c2n(char c)
        {
            if( c >= '0' && c <= '9' )
                return c - '0';
            if( c >= 'a' && c <= 'f' )
                return c - 'a' + 10;
            if( c >= 'A' && c <= 'F' )
                return c - 'A' + 10;
            return 0;
        }

    private:
        const char *const _str_begin;
        const char *_str;
    };

    template <class XINT>
    XINT hex2xint(const char *str)
    {
        XINT n;
        hex_reader<XINT> xreader( str );
        for_each_unit_r( n, xreader );
        return n;
    }

} // namespace xintlib


#endif	/* _XINTLIBIO_H */
